<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\library\Ems as Emslib;
use app\common\model\User;

/**
 * 邮箱验证码接口
 */
class Ems extends Api
{
    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();
        \think\Hook::add('ems_send', function ($params) {
            $obj = \app\common\library\Email::instance();
            $result = $obj
                ->to($params->email)
                ->subject(__('Verification code'))
                ->message(__("Your verification code is:") . $params->code)
                ->send();
            return $result;
        });
    }

    public function index()
    {
        $email = $this->request->request("email");
        $event = $this->request->request("event");
        $event = $event ? $event : 'register';

        $last = Emslib::get($email, $event);
        if ($last && time() - $last['createtime'] < 60) {
            $this->error(__('Send frequently'));
        }
        if ($event) {
            $userinfo = User::getByEmail($email);
            if ($event == 'register' && $userinfo) {
                //已被注册
                $this->error(__('Already registered'));
            } elseif (in_array($event, ['changeemail']) && $userinfo) {
                //被占用
                $this->error(__('Already occupied'));
            } elseif (in_array($event, ['changepwd', 'resetpwd']) && !$userinfo) {
                //未注册
                $this->error(__('Unregistered'));
            }
        }
        $ret = Emslib::send($email, null, $event);
        if ($ret) {
            $this->success(__('Sent successfully'));
        } else {
            $this->error(__('Sent failed'));
        }

//        if ($_POST) {
//            $mobile = input('phone');
//            $code = input('code', rand(1000, 9999));
//
//            $site = config('site');
//            if (!$site['dxjk']['key']) {
//                echo json_encode(['ok' => '1', 'data' => $code, 'msg' => __('SMS simulation sent successfully.Verification code：') . $code]);
//                exit();
//            }
//            $sendUrl = 'http://v.juhe.cn/sms/send'; //短信接口的URL
//            $smsConf = array(
//                'key' => $site['dxjk']['key'],//'f5cd3c466161918a823ed1c5d58cec58', //您申请的APPKEY
//                'mobile' => $mobile, //接受短信的用户手机号码
//                'tpl_id' => $site['dxjk']['tpl_id'], //您申请的短信模板ID，根据实际情况修改
//                'tpl_value' => '#code#=' . $code //您设置的模板变量，根据实际情况修改
//            );
//            $content = $this->juhecurl($sendUrl, $smsConf, 1); //请求发送短信
//            if ($content) {
//                $result = json_decode($content, true);
//                $error_code = $result['error_code'];
//                if ($error_code == 0) {
//                    //状态为0，说明短信发送成功
//                    //Cookie::set('yzmcode',md5($code));//验证码保存到SESSION中
//                    $sms = \app\common\model\Sms::where(['mobile' => $mobile])->delete();
//                    $sms = \app\common\model\Sms::create(['event' => 'register', 'mobile' => $mobile, 'code' => $code, 'ip' => request()->ip(), 'createtime' => time()]);
//                    echo json_encode(['ok' => '1', 'data' => $code, 'msg' => __('SMS sent successfully')]);
//                    //echo json_encode(['ok' => '1','data'=>md5($code.'6db'),'msg' => '短信发送成功,短信ID：'.$result['result']['sid']]);
//                } else {
//                    //状态非0，说明失败
//                    $msg = $result['reason'];
//                    echo json_encode(['ok' => '2', 'data' => md5($code), 'msg' => __('SMS sent failed') . '(' . $error_code . ')：' . $msg]);
//                }
//            } else {
//                //返回内容异常，以下可根据业务逻辑自行修改
//                echo json_encode(['ok' => '2', 'data' => md5($code), 'msg' => __('Request to send SMS failed')]);
//            }
//        }

    }

    public function dxb()
    {
        $statusStr = array(
            "0" => "短信发送成功",
            "-1" => "参数不全",
            "-2" => "服务器空间不支持,请确认支持curl或者fsocket，联系您的空间商解决或者更换空间！",
            "30" => "密码错误",
            "40" => "账号不存在",
            "41" => "余额不足",
            "42" => "帐户已过期",
            "43" => "IP地址限制",
            "50" => "内容含有敏感词"
        );

        $mobile = input('phone');
        $code = input('code', rand(1000, 9999));

        $smsapi = "http://api.smsbao.com/";
        $user = "zhangbin1"; //短信平台帐号
        $pass = md5("zhangbin12"); //短信平台密码
        $content = "【咕咕网络】您的验证码是" . $code . "。如非本人操作，请忽略本短信";//要发送的短信内容
        $phone = $mobile;//要发送短信的手机号码
        $sendurl = $smsapi . "sms?u=" . $user . "&p=" . $pass . "&m=" . $phone . "&c=" . urlencode($content);
        $result = file_get_contents($sendurl);
        if ($result == 0) {
            $sms = \app\common\model\Sms::where(['mobile' => $mobile])->delete();
            $sms = \app\common\model\Sms::create(['event' => 'register', 'mobile' => $mobile, 'code' => $code, 'ip' => request()->ip(), 'createtime' => time()]);
            echo json_encode(['ok' => '1', 'data' => $code, 'msg' => '短信发送成功']);
        } else {
            echo json_encode(['ok' => '1', 'data' => $code, 'msg' => $statusStr[$result]]);
        }
    }

    /**
     * 发送验证码
     *
     * @param string $email 邮箱
     * @param string $event 事件名称
     */
    public function send()
    {
        $email = $this->request->request("email");
        $event = $this->request->request("event");
        $event = $event ? $event : 'register';

        $last = Emslib::get($email, $event);
        if ($last && time() - $last['createtime'] < 60) {
            $this->error(__('Send frequently'));
        }
        if ($event) {
            $userinfo = User::getByEmail($email);
            if ($event == 'register' && $userinfo) {
                //已被注册
                $this->error(__('Already registered'));
            } elseif (in_array($event, ['changeemail']) && $userinfo) {
                //被占用
                $this->error(__('Already occupied'));
            } elseif (in_array($event, ['changepwd', 'resetpwd']) && !$userinfo) {
                //未注册
                $this->error(__('Unregistered'));
            }
        }
        $ret = Emslib::send($email, null, $event);
        if ($ret) {
            $this->success(__('Sent successfully'));
        } else {
            $this->error(__('Sent failed'));
        }
    }

    /**
     * 检测验证码
     *
     * @param string $email 邮箱
     * @param string $event 事件名称
     * @param string $captcha 验证码
     */
    public function check()
    {
        $email = $this->request->request("email");
        $event = $this->request->request("event");
        $event = $event ? $event : 'register';
        $captcha = $this->request->request("captcha");

        if ($event) {
            $userinfo = User::getByEmail($email);
            if ($event == 'register' && $userinfo) {
                //已被注册
                $this->error(__('Already registered'));
            } elseif (in_array($event, ['changeemail']) && $userinfo) {
                //被占用
                $this->error(__('Already occupied'));
            } elseif (in_array($event, ['changepwd', 'resetpwd']) && !$userinfo) {
                //未注册
                $this->error(__('Unregistered'));
            }
        }
        $ret = Emslib::check($email, $captcha, $event);
        if ($ret) {
            $this->success(__('Sent successfully'));
        } else {
            $this->error(__('Incorrect verification code'));
        }
    }
}
